<?php
/*
* 无想一刀:atongmz@foxmail.com
* gitee: https://gitee.com/Jane_kang
*腾讯云解析api接口
*文档地址 https://cloud.tencent.com/document/api/302/4032
*问题反馈以及建议请联系我的邮箱
*/
class tencent {
	private $SecretId;
	private $SecretKey;
	private $endpoint = "cns.api.qcloud.com"; //接口请求域名
	

	/**
	 * SecretId 腾讯云SecretId
	 * SecretKey 腾讯云SecretKey
	*/
	function __construct($SecretId, $SecretKey){
        $this->SecretId = $SecretId;
        $this->SecretKey = $SecretKey;
    }
    
    /**
	 * 查询域名列表
	 * length 返回数量，默认20，最大值100
	*/
    public function DomainList($length=100){
	   $data['Action']=__FUNCTION__;
	   $data['length']=$length;
	   $data['SecretId']=$this->SecretId;
       $data['Timestamp']=time();
       $data['Nonce']=mt_rand(10000,99999);
       $data['SignatureMethod']='HmacSHA256';
       $data['Signature']=$this->sign($data);
       return $this->curl_post($data);
	}
	
	/**
	 * 查询域名解析记录列表
	 * domain 要操作的域名,主域名，不包括 www，例如：qcloud.com(必填)
	 * subDomain 过滤条件,根据子域名进行过滤
	 * recordType 过滤条件,根据记录类型进行过滤
	*/
    public function RecordList($domain){
	   $data['Action']=__FUNCTION__;
	   $data['domain']=$domain;
       $data['SecretId']=$this->SecretId;
       $data['Timestamp']=time();
       $data['Nonce']=mt_rand(10000,99999);
       $data['SignatureMethod']='HmacSHA256';
       $data['Signature']=$this->sign($data);
       return $this->curl_post($data);
	}
	/**
	 * 添加解析记录
	 * domain 要添加解析记录的域名,主域名，不包括 www，例如：qcloud.com(必填)
	 * subDomain 子域名前缀，例如：www (必填)
	 * recordType 记录类型，可选的记录类型为："A", "CNAME", "MX", "TXT", "NS", "AAAA", "SRV" (必填)
	 * recordLine 记录的线路名称，例如："默认" (必填)
	 * value 记录值，例如 IP：192.168.10.2，CNAME：cname.dnspod.com.，MX：mail.dnspod.com.(必填)
	 * mx  MX 优先级，范围为0 - 50，当 recordType 选择 MX 时，mx 参数必选 
	*/
    public function RecordCreate($array){
	   $data['Action']=__FUNCTION__;
	   $data['domain']=$array['domain'];
	   $data['subDomain']=$array['subDomain'];
	   $data['recordType']=$array['recordType'];
	   $data['recordLine']='默认';
	   $data['value']=$array['value'];
	   $data['mx']=$array['mx'];
       $data['SecretId']=$this->SecretId;
       $data['Timestamp']=time();
       $data['Nonce']=mt_rand(10000,99999);
       $data['SignatureMethod']='HmacSHA256';
       $data['Signature']=$this->sign($data);
       return $this->curl_post($data);
	}
	
	
		/**
	 * 修改解析记录
	 * domain 要修改解析记录的域名,主域名，不包括 www，例如：qcloud.com(必填)
	 * subDomain 子域名前缀，例如：www (必填)
	 * recordType 记录类型，可选的记录类型为："A", "CNAME", "MX", "TXT", "NS", "AAAA", "SRV" (必填)
	 * recordLine 记录的线路名称，例如："默认" (必填)
	 * value 记录值，例如 IP：192.168.10.2，CNAME：cname.dnspod.com.，MX：mail.dnspod.com.(必填)
	 * mx  MX 优先级，范围为0 - 50，当 recordType 选择 MX 时，mx 参数必选 
	*/
    public function RecordModify($array){
	   $data['Action']=__FUNCTION__;
	   $data['domain']=$array['domain'];
	   $data['subDomain']=$array['subDomain'];
	   $data['recordType']=$array['recordType'];
	   $data['recordLine']='默认';
	   $data['value']=$array['value'];
	   $data['mx']=$array['mx'];
       $data['SecretId']=$this->SecretId;
       $data['Timestamp']=time();
       $data['Nonce']=mt_rand(10000,99999);
       $data['SignatureMethod']='HmacSHA256';
       $data['Signature']=$this->sign($data);
       return $this->curl_post($data);
	}


    //加密
    public function sign($data){
	  $res=$this->array_prarm($data);
      $srcStr = 'POST'.$this->endpoint.'/v2/index.php?'.$res;
      $sign= base64_encode(hash_hmac('sha256', $srcStr, $this->SecretKey, true));
      return $sign;
	}
	
	
	//参数排序
	public function array_prarm($array) {
	ksort($array);
	$prarms = [];
	foreach ($array as $key => $val) {
		$prarms[] = $key . '=' . urlencode($val);
	}
	return implode('&', $prarms);
    }
	
	//post请求
	private function curl_post($data){
		$url = 'https://'.$this->endpoint.'/v2/index.php?';
		$ch=curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$json=curl_exec($ch);
		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		if($httpCode==200){
			$arr=json_decode($json,true);
			return $arr;
		}else{
			return false;
		}
	}
	
}
